package com.chinairi.mall.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinairi.common.model.ApprovalModel;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.UCIVO;
import com.chinairi.mall.controller.viewobject.UserCompanyInfoVO;
import com.chinairi.mall.model.UserCompanyInfo;
import com.chinairi.mall.service.IUserCompanyInfoService;
import com.chinairi.mall.service.modelobject.CompanyTypeMO;
import com.chinairi.mall.service.modelobject.OCTAForSPMO;
import com.chinairi.mall.service.modelobject.ShopInfoToCompRedisMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;
import com.chinairi.common.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户企业信息表 前端控制器
 * </p>
 *
 * @author Webgasd
 * @since 2020-09-16
 */
@RestController
@RequestMapping("/user-company-info")
public class UserCompanyInfoController extends BaseController {

    private static final long serialVersionUID = 2781276168020775466L;

    @Autowired
    private IUserCompanyInfoService iUserCompanyInfoService;

    /**
     * 插入企业信息
     *
     * @param request
     * @param registerJson
     * @throws Exception
     */
    @PostMapping("/add")
    public void add(@RequestBody String registerJson, HttpServletRequest request) throws Exception {
        UserCompanyInfo userCompanyInfo = JSON.parseObject(registerJson, UserCompanyInfo.class);
        ResponseUtil.showMessage("success", "更新完成", iUserCompanyInfoService.add(userCompanyInfo,UserSession.getUserId()));

    }

    /**
     * 更新企业信息
     *
     * @param userCompanyInfo
     * @throws Exception
     */
    @PostMapping("/update")
    public void update(@RequestBody UserCompanyInfo userCompanyInfo) throws Exception {
        iUserCompanyInfoService.update(userCompanyInfo,UserSession.getUserId());
        ResponseUtil.showMessage("success", "更新完成", "");

    }

    /**
     * 根据用户id获取企业信息
     *
     * @throws Exception
     */
    @GetMapping("/getCom")
    public void getCom(Integer type) throws Exception {
        CompanyTypeMO companytypeMo = iUserCompanyInfoService.getCom(type);
        ResponseUtil.showMessage("success", "查询完成", companytypeMo);
    }

    /**
     * 获取企业列表
     * 前端无输入条件时，获取全部企业
     * 有输入条件时，按条件返回
     *
     * @param companyTypeMo
     * @throws IOException
     */
    @GetMapping("/getComList")
    public void getComList(CompanyTypeMO companyTypeMo) throws IOException {
        List<CompanyTypeMO> companyTypeMoList = iUserCompanyInfoService.getComList(companyTypeMo);
        ResponseUtil.showMessage("success", "获取完成", companyTypeMoList);
    }

    /**
     * 更新企业审核状态
     * @param companyTypeMo
     * @throws IOException
     */
    @PostMapping("/changeStatus")
    public void changeStatus(@RequestBody CompanyTypeMO companyTypeMo) throws IOException {
        iUserCompanyInfoService.changeStatus(companyTypeMo);
        ResponseUtil.showMessage("success", "资质状态改变成功", null);
    }

    /**
     * 限时拉黑企业
     * @param companyTypeMo
     * @throws IOException
     */
    @PostMapping("/deleteStatus")
    public void deleteStatus(@RequestBody CompanyTypeMO companyTypeMo) throws IOException {
        iUserCompanyInfoService.deleteStatus(companyTypeMo);
        ResponseUtil.showMessage("success", "资质拉黑成功", null);
    }

    /**
     * 审批流回溯地址
     * @param approvalModel
     * @throws IOException
     */
    @PutMapping("/updateFlowable")
    @ResponseBody
    public void updateFlowable(@RequestBody ApprovalModel approvalModel) throws IOException {
        iUserCompanyInfoService.updateFlowable(approvalModel);
        ResponseUtil.showMessage("success","提交成功",null);
    }

    /**
     * 获取店铺和企业信息
     * @param csDbId
     * @throws IOException
     */
    @GetMapping("/getShopInfoAndCompInfo")
    public void getShopInfoAndCompInfo(String csDbid) throws IOException {
        ShopInfoToCompRedisMO shopInfoToCompRedisMo = iUserCompanyInfoService.getShopInfoAndCompInfo(csDbid);
        ResponseUtil.showMessage("success", "获取完成", shopInfoToCompRedisMo);
    }

    /**
     * 获取企业归属下拉列表
     * @return
     */
    @GetMapping("/listBelongTo")
    public List<Map<String,Object>> listBelongTo(){
        return iUserCompanyInfoService.getBelongToList();
    }
}
